package com.crrc.sandameter.view

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.crrc.sandameter.R

class TableAdapter(private val items: MutableList<TableItem>,
    private val onItemClickListener: OnItemClickListener) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        const val VIEW_TYPE_CROSSED_CELL = 0
        const val VIEW_TYPE_COLUMN_HEADER = 1
        const val VIEW_TYPE_ROW_HEADER = 2
        const val VIEW_TYPE_DATA_CELL = 3
    }

    // 定义点击监听器接口
    interface OnItemClickListener {
        fun onDataCellClick(adapterPosition: Int, data: TableItem) // 传递适配器位置和数据
    }

    override fun getItemViewType(position: Int): Int {
        return when (items[position]) {
            is TableItem.CrossedCell -> VIEW_TYPE_CROSSED_CELL
            is TableItem.ColumnHeader -> VIEW_TYPE_COLUMN_HEADER
            is TableItem.RowHeader -> VIEW_TYPE_ROW_HEADER
            else -> VIEW_TYPE_DATA_CELL
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            VIEW_TYPE_CROSSED_CELL -> {
                val view = inflater.inflate(R.layout.item_crossed_cell, parent, false)
                CrossedCellViewHolder(view)
            }
            VIEW_TYPE_COLUMN_HEADER -> {
                val view = inflater.inflate(R.layout.item_column_header, parent, false)
                ColumnHeaderViewHolder(view)
            }
            VIEW_TYPE_ROW_HEADER -> {
                val view = inflater.inflate(R.layout.item_row_header, parent, false)
                RowHeaderViewHolder(view)
            }
            VIEW_TYPE_DATA_CELL -> {
                val view = inflater.inflate(R.layout.item_data_cell, parent, false)
                DataCellViewHolder(view, onItemClickListener)
            }
            else -> throw IllegalArgumentException("Unknown view type")
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (val item = items[position]) {
            is TableItem.CrossedCell -> { // CrossedCellViewHolder does not need specific binding logic if its content is static
            }
            is TableItem.ColumnHeader -> {
                (holder as ColumnHeaderViewHolder).bind(item)
            }
            is TableItem.RowHeader -> {
                (holder as RowHeaderViewHolder).bind(item)
            }
            is TableItem.DataCell -> {
                (holder as DataCellViewHolder).bind(item)
            }
        }
    }

    override fun getItemCount(): Int = items.size

    class CrossedCellViewHolder(itemView: View) :
        RecyclerView.ViewHolder(itemView) { // No specific binding needed as its content is static in XML
    }

    class ColumnHeaderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val textView: TextView = itemView.findViewById(R.id.column_header_text)
        fun bind(item: TableItem.ColumnHeader) {
            textView.text = item.vhecileNo
        }
    }

    class RowHeaderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val textView: TextView = itemView.findViewById(R.id.row_header_text)
        fun bind(item: TableItem.RowHeader) {
            textView.text = item.position
        }
    }

    class DataCellViewHolder(itemView: View, private val listener: OnItemClickListener) :
        RecyclerView.ViewHolder(itemView) {
        private val textView: TextView = itemView.findViewById(R.id.data_cell_text)

        @SuppressLint("SetTextI18n")
        fun bind(item: TableItem.DataCell) {
            if (item.testValue.length > 1) {
                itemView.setBackgroundColor(itemView.context.getColor(R.color.green))
                textView.setTextColor(itemView.context.getColor(R.color.white))
            } else {
                itemView.setBackgroundColor(itemView.context.getColor(R.color.white))
                textView.setTextColor(itemView.context.getColor(R.color.black))
            }
            if (item.isOperateCell == "1") {
                textView.text = "${item.testValue}${item.resultUnit}"
            } else {
                textView.text = "-"
            }
            itemView.setOnClickListener {
                if (item.isOperateCell == "1") {
                    listener.onDataCellClick(adapterPosition, item)
                }
            }
        }
    }

    fun updateDataCell(data: TableItem, position: Int, newText: String) {
        if (data is TableItem.DataCell) {
            val currentItem = items[position]
            if (currentItem is TableItem.DataCell) { // Create a new DataCell instance with updated text (data classes are immutable)
                currentItem.testValue = newText
                notifyItemChanged(position) // 通知 RecyclerView 刷新该位置的 Item
            }
        }
    }
}